package com.yaozheng.vocationaltraining.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.bean.CarCategoryBean;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;

import java.util.List;

/**
 * Created by moon on 2017/11/12.
 */

public class CarCategoryListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;//
    private Activity mContext;
    private BitmapLoader userBitmapLoader;

    private List<CarCategoryBean> data = null;

    private int selectedPosition = -1;

    public CarCategoryListAdapter(Activity context,
                                  List<CarCategoryBean> data) {
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.data = data;
        userBitmapLoader = new BitmapLoader(Glide.with(context), R.drawable.user_img);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(
                    R.layout.car_category_list_item, null);
            holder = new ViewHolder();

            holder.car_category_imageview = (ImageView) convertView
                    .findViewById(R.id.car_category_imageview);
            holder.car_category_name_textview = (TextView) convertView
                    .findViewById(R.id.car_category_name_textview);


            convertView.setTag(holder);//
        } else {
            holder = (ViewHolder) convertView.getTag();//
        }

        //加载图片
        userBitmapLoader.displayImage(data.get(position).getImage(), holder.car_category_imageview);
        holder.car_category_name_textview.setText(data.get(position).getName());

        return convertView;
    }

    public void setData(List<CarCategoryBean> data) {
        this.data = data;
    }

    public void setselectedPosition(int selectedPosition) {
        notifyDataSetChanged();;
    }

    class ViewHolder {
        ImageView car_category_imageview;
        TextView car_category_name_textview;
    }
}
